package com.itheima.jdbctemplate;

import com.itheima.domain.Account;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


public class JdbcTemplateDemo02 {

    /*通过配置文件获取对象*/
    public static void main(String[] args) {
        //获取容器
      ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //获取对象
        JdbcTemplate jt = ac.getBean("jdbcTemplate", JdbcTemplate.class);


        //保存
        jt.update("insert into account(name,money) values(?,?)","eee",2000);

        //更新
        jt.update("update account set name=?,money=? where id=? ","eee",800,3);

        //删除
        jt.update("delete from account where id=?",2);

        //查询方法
        List<Account> accounts = jt.query("select * from account where money>?",
                new AccountRowMapper(), 500);
        for (Account account : accounts) {
            System.out.println(account);
        }


    }

}
/*自定义结果集封装类*/
class AccountRowMapper implements RowMapper<Account>{

    public Account mapRow(ResultSet resultSet, int i) throws SQLException {
        Account account = new Account();
        account.setId(resultSet.getInt("id"));
        account.setName(resultSet.getString("name"));
        account.setMoney(resultSet.getFloat("money"));
        return account;
    }


    /*spring 提供的封装类*/



}